package org.jdesktop.swingx.input;

import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.geom.Point2D;
import java.util.Vector;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.WindowConstants;

import org.jdesktop.swingx.JXMapViewer;

import vnreal.ZNEW.GraphTest.SubstrateLink;
import vnreal.gui.menu.MapMenu;

/**
 * Centers the map on the mouse cursor if Right is double-clicked or middle
 * mouse button is pressed.
 * 
 * @author Martin Steiger
 * @author joshy
 */
public class EditLink {
	JDialog dialog;
	private JXMapViewer viewer;

	public EditLink(JXMapViewer viewer) {
		this.viewer = viewer;
		openDialog(viewer);
		fillFields();
	}

	SubstrateLink link1 = new SubstrateLink();
	SubstrateLink link2 = new SubstrateLink();

	private ActionListener cancelClicked = new ActionListener() {
		public void actionPerformed(ActionEvent actionEvent) {
			NewNodeMapListener.dialog.setEnabled(true);
			dialog.dispose();
		}
	};
	private ActionListener okClicked = new ActionListener() {
		public void actionPerformed(ActionEvent actionEvent) {
			boolean isNumber = true;
			NewNodeMapListener.dialog.setEnabled(true);
			try {
				Double.valueOf(bandwidth.getText());
				Double.valueOf(length.getText());
			} catch (NumberFormatException ex) {
				isNumber = false;
			} catch (NullPointerException ex) {
				isNumber = false;
			}
			if (isNumber) {
				link1.bandwidth = bandwidth.getText();
				link2.bandwidth = bandwidth.getText();
				link1.length = length.getText();
				link2.length = length.getText();
				link1.typeOfLink = (String) type.getSelectedItem();
				link2.typeOfLink = (String) type.getSelectedItem();
				dialog.dispose();
			} else {
				JOptionPane.showMessageDialog(dialog,
						"Error!", "Error!",
						JOptionPane.WARNING_MESSAGE);
			}
		}
	};
	private JLabel destination;
	private JTextField bandwidth;
	private JComboBox type;
	private JTextField length;
	private void fillFields() {
		for(SubstrateLink link : NewNodeMapListener.substrateLinks) {
			if(link.src != null) {
				if(link.src.globalNodeId.equals(NewNodeMapListener.selected)) {
					destination.setText(link.src.globalNodeId);
					bandwidth.setText(link.bandwidth);
					type.setSelectedItem(link.typeOfLink);
					length.setText(link.length);
					link1 = link;
				}
			}
			if(link.dst != null) {
				if(link.dst.globalNodeId.equals(NewNodeMapListener.selected)) {
					link2 = link;
				}
			}
		}
	}
	private void openDialog(JXMapViewer viewer) {
		dialog = new JDialog(MapMenu.frame, "Add Link");
		JLabel label;
		dialog.setLayout(new GridLayout(6,2));
		dialog.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);

		label = new JLabel("   Destination");
		dialog.add(label);

		destination = new JLabel();
		dialog.add(destination);

		label = new JLabel("   Bandwidth");
		dialog.add(label);

		bandwidth = new JTextField();
		dialog.add(bandwidth);

		label = new JLabel("   Type of Link");
		dialog.add(label);

		type = new JComboBox(new Object[] { "Fiber", "Copper" });
		dialog.add(type);

		label = new JLabel("   Length");
		dialog.add(label);

		length = new JTextField();
		dialog.add(length);

		dialog.add(new JLabel());
		dialog.add(new JLabel());

		JButton okButton = new JButton("OK");
		okButton.addActionListener(okClicked);
		dialog.add(okButton);

		JButton cancelButton = new JButton("Cancel");
		cancelButton.addActionListener(cancelClicked);
		dialog.add(cancelButton);

		dialog.setSize(500, 200);
		dialog.setResizable(false);
		// dialog.setAlwaysOnTop(true);
		dialog.setVisible(true);
	}
}
